﻿using System;
using System.Net.Http;
using System.Threading.Tasks;
using IdentityModel.Client;
using Newtonsoft.Json.Linq;

namespace ResourceOwnerClient
{
    class Program
    {
        static void Main(string[] args) => MainAsync().GetAwaiter().GetResult();
        private static async Task MainAsync()
        {
            var client = new HttpClient();
            var disco = client.GetDiscoveryDocumentAsync("http://localhost:57446");

            if (disco.Result.IsError)
            {
                Console.WriteLine(disco.Result.IsError);
                return;
            }
            var tokenResponse = await client.RequestPasswordTokenAsync(new PasswordTokenRequest
            {
                Address = disco.Result.TokenEndpoint,
                ClientId = "li.client",
                ClientSecret = "secret",

                UserName = "li",
                Password = "123456",
                //Scope="api1"
            });
            if (tokenResponse.IsError)
            {
                Console.WriteLine(tokenResponse.IsError);
                return;
            }
            Console.WriteLine(tokenResponse.Json);
            Console.WriteLine("\n\n");

            var apiClient = new HttpClient();
            apiClient.SetBearerToken(tokenResponse.AccessToken);

            var response = await apiClient.GetAsync("http://localhost:58119/api/identity");
            if (!response.IsSuccessStatusCode)
            {
                Console.WriteLine(response.StatusCode);
            }
            else
            {
                var content = response.Content.ReadAsStringAsync().Result;
                Console.WriteLine(JArray.Parse(content));
            }

            #region 注释代码
            //var tokenResponse2 = await client.RequestPasswordTokenAsync(new PasswordTokenRequest
            //{
            //    Address = disco.Result.TokenEndpoint,
            //    ClientId = "li.client",
            //    ClientSecret = "secret",

            //    UserName = "li",
            //    Password = "123456",
            //    Scope = "api2"
            //});
            //if (tokenResponse2.IsError)
            //{
            //    Console.WriteLine(tokenResponse2.IsError);
            //    return;
            //}
            //Console.WriteLine(tokenResponse2.Json);
            //Console.WriteLine("\n\n");
            //apiClient.SetBearerToken(tokenResponse2.AccessToken); 
            #endregion
            //58458
            var response2 = await apiClient.GetAsync("http://localhost:58458/api/identity/Get");
            if (!response2.IsSuccessStatusCode)
            {
                Console.WriteLine(response2.StatusCode);
            }
            else
            {
                var content = response2.Content.ReadAsStringAsync().Result;
                Console.WriteLine(JObject.Parse(content));
            }
            var response3 = await apiClient.GetAsync("http://localhost:58458/api/identity/GetPermission");
            if (!response3.IsSuccessStatusCode)
            {
                Console.WriteLine(response3.StatusCode);
            }
            else
            {
                var content = response3.Content.ReadAsStringAsync().Result;
                Console.WriteLine(JObject.Parse(content));
            }
        }
    }
}
